package com.dingup.provider.controller.classInfo;


import com.dingup.api.Util.RedisUtils;
import com.dingup.api.base.HttpResContent;
import com.dingup.api.bean.page.PageData;
import com.dingup.api.bean.requestBean.classInfo.ClassInfoBean;
import com.dingup.api.bean.requestBean.classInfo.ClassPageBean;
import com.dingup.api.bean.response.ResponseSelectBean;
import com.dingup.api.bean.response.SelectBean;
import com.dingup.api.enums.ClassStatusEnum;
import com.dingup.api.model.classInfo.ClassInfo;
import com.dingup.api.model.lesson.LessonInfo;
import com.dingup.api.model.sys.SysUser;
import com.dingup.provider.feign.client.paike.classInfo.PaikeClassInfoFeignClient;
import com.dingup.provider.service.classInfo.ClassInfoService;
import com.dingup.provider.service.lesson.LessonService;
import com.dingup.provider.service.sys.SysUserService;
import com.suyeer.basic.util.BHttpResUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/8/24.
 */
@Controller
@RequestMapping("/class")
public class ClassController {
    private static Logger logger = LoggerFactory.getLogger(ClassController.class);

    private final String CLASS_TEACHER_NAME = "班主任";

    private final String TEACHER_NAME = "老师";
    @Autowired
    ClassInfoService classInfoService;
    @Autowired
    LessonService lessonService;
    @Autowired
    PaikeClassInfoFeignClient paikeClassInfoFeignClient;
    @Autowired
    SysUserService sysUserService;

    @RequestMapping(value = "/get_teacher.do",method = RequestMethod.POST)
    public void getTeacher(HttpServletRequest request,
                           HttpServletResponse response,
                           Long schoolCampusId,
                           Boolean ifClassTeacher) {
        HttpResContent<List<ResponseSelectBean>> hrc = new HttpResContent<>(request, response);
        String desc = "";
        if (ifClassTeacher) {
            desc = "获取班主任";
        } else {
            desc = "获取老师";
        }
        hrc.setFuncDes(desc);
        try {
            String roleName = null;
            if (ifClassTeacher) {
                roleName = CLASS_TEACHER_NAME;
            } else {
                roleName = TEACHER_NAME;
            }
            if (StringUtils.isEmpty(roleName)) {
                throw new Exception("无效的老师类型");
            }
            List<SysUser> list = classInfoService.getTeacher(schoolCampusId, roleName);
            List<ResponseSelectBean> responseSelectBeens = new ArrayList<>();
            list.stream().forEach(
                    user -> {
                        responseSelectBeens.add(new ResponseSelectBean(user.getUserName(), user.getUserId()));
                    }
            );
            saveSuccessMsgAndData(hrc, responseSelectBeens);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_all_user_select.do", method = RequestMethod.POST)
    public void getStudent(HttpServletRequest request,
                           HttpServletResponse response) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取学生");
        try {
            List<SelectBean> res = sysUserService.getAllUserNameAndUserIds();
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson.do",method = RequestMethod.POST)
    public void getLessonExamSubject(HttpServletRequest request,
                                     HttpServletResponse response) {
        HttpResContent<List<ResponseSelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取课程的简要信息");
        try {
            List<String> myEmployee = RedisUtils.getInstance().getDataPermissionEmployee(request);
            List<LessonInfo> list = lessonService.getByCreateUserIds(myEmployee);
            List<ResponseSelectBean> responseSelectBeens = new ArrayList<>();
            list.stream().forEach(
                    lessonInfo -> {
                        ResponseSelectBean bean = new ResponseSelectBean(lessonInfo.getLessonName(), String.valueOf(lessonInfo.getId()));
                        bean.setLessonCampus(lessonInfo.getLessonCampus());
                        responseSelectBeens.add(bean);
                    }
            );
            saveSuccessMsgAndData(hrc, responseSelectBeens);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/save_class_info.do",method = RequestMethod.POST)
    public void saveLessonInfo(HttpServletRequest request,
                               HttpServletResponse response,
                               ClassInfoBean classInfoBean) {
        HttpResContent<ClassInfo> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("保存班级信息");
        try {
            ClassInfo classInfo = classInfoService.getByClassName(classInfoBean.getClassName());
            if (classInfo != null) {
                throw new Exception("班级名重读");
            }
            String currentUserId = RedisUtils.getInstance().getUserId(request);
            classInfoBean.setCreateUserId(currentUserId);
            ClassInfo res = classInfoService.saveClassInfo(classInfoBean);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_class_info.do",method = RequestMethod.POST)
    public void updateClassInfo(HttpServletRequest request,
                                 HttpServletResponse response,
                                 ClassInfoBean classInfoBean) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改班级信息");
        try {
            ClassInfo classInfo = classInfoService.getByClassName(classInfoBean.getClassName());
            if (classInfo != null && !classInfo.getId().equals(classInfoBean.getId())) {
                throw new Exception("班级名重复");
            }
            String currentUserId = RedisUtils.getInstance().getUserId(request);
            classInfoService.updateClassInfo(classInfoBean,currentUserId);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_class_info.do",method = RequestMethod.POST)
    public void getLessonInfo(HttpServletRequest request,
                              HttpServletResponse response,
                              ClassPageBean classPageBean) {
        HttpResContent<PageData> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("筛选班级信息");
        try {
            List<String> myEmployee = RedisUtils.getInstance().getDataPermissionEmployee(request);
            PageData pageData = classInfoService.getClassInfo(classPageBean, myEmployee);
            saveSuccessMsgAndData(hrc, pageData);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_class_info_search.do",method = RequestMethod.POST)
    public void getLessonInfoSearch(HttpServletRequest request,
                                    HttpServletResponse response,
                                    ClassPageBean classPageBean) {
        HttpResContent<PageData> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("班级搜搜框列表");
        try {
            List<String> myEmployee = RedisUtils.getInstance().getDataPermissionEmployee(request);
            PageData pageData = classInfoService.getClassInfo(classPageBean.getPage(), classPageBean.getRows(), classPageBean.getQuery(), myEmployee);
            saveSuccessMsgAndData(hrc, pageData);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
    @RequestMapping(value = "/get_class_info_msg_by_id.do",method = RequestMethod.POST)
    public void getLessonInfoById(HttpServletRequest request,
                                    HttpServletResponse response,
                                    Integer classId) {
        HttpResContent<ClassInfo> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取班级的详细信息");
        try {
            ClassInfo classInfo = classInfoService.getClassInfo(classId);

            saveSuccessMsgAndData(hrc, classInfo);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
    @RequestMapping(value = "/update_class_status_end.do",method = RequestMethod.POST)
    public void updateClass(HttpServletRequest request,
                                  HttpServletResponse response,
                                  Integer classId) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("班级结课");
        try {
            String userId = RedisUtils.getInstance().getUserId(request);
            classInfoService.updateClassStatus(classId, ClassStatusEnum.CLASS_END,userId);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
    @RequestMapping(value = "/delete_class.do",method = RequestMethod.POST)
    public void deleteClass(HttpServletRequest request,
                            HttpServletResponse response,
                            Integer classId) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("删除班级");
        try {
            String userId = RedisUtils.getInstance().getUserId(request);
            classInfoService.deleteClass(classId,userId);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
}
